﻿namespace SAD.DataCenter
{
    using System;
    using System.Linq;
    using SAD.Common.Models;
    using System.Collections.Generic;

    public class InfoCategoryManagerService : SAD.Common.Contracts.DataCenter.IInfoCategoryManager
    {
        public InfoCategory_Data[] GetInfoCategorys(int showNodeID)
        {
            return InfoCategoryManager.Instance.GetInfoCategorys(showNodeID);
        }
    }

    public class InfoCategoryManager
    {
        #region Instance
        /// <summary>
        /// 类型单例
        /// </summary>
        public static InfoCategoryManager Instance
        {
            get
            {
                return m_Instance;
            }
        }
        private static InfoCategoryManager m_Instance = new InfoCategoryManager();
        #endregion

        public InfoCategory_Data[] GetInfoCategorys(int showNodeID)
        {
            List<InfoCategory_Data> categoryList = null;
            using (SAD_Modles ctx = new SAD_Modles())
            {
                var r = from rel in ctx.ShowNode_InfoCategoryRels
                        from category in ctx.InfoCategory_Datas
                        where rel.InfoCategoryID == category.ID && rel.ShowNodeID == showNodeID
                        orderby rel.Sort ascending
                        select category;

                categoryList = r.ToList();
            }

            categoryList.Insert(0, new InfoCategory_Data() { ID = 0, CodeName = "", DisplayName = "所有分类", Describe = "", ParentID = 0, AddTime = DateTime.Now });

            return categoryList.ToArray();
        }
    }
}